package top.aicey.ai.sparring.common.utils;

import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Maps;

import top.aicey.ai.sparring.common.holder.CurrentUser;
import top.aicey.ai.sparring.common.holder.SparringContext;
import top.aicey.ai.sparring.dto.enums.SparringStatusEnum;
import top.aicey.ai.sparring.dto.req.GenerateAIAgentCallRequestDto;

/**
 * @author baiyang
 */
public class SparringContextUtils {

    public static SparringContext buildSparringContext(GenerateAIAgentCallRequestDto dto, CurrentUser user){

        String templateConfig = dto.getTemplateConfig();

        JSONObject jsonObject = JSON.parseObject(templateConfig);

        JSONObject voiceChat = jsonObject.getJSONObject("VoiceChat");
        JSONObject bailianAppParams = voiceChat.getJSONObject("BailianAppParams");

        JSONObject bizParams = bailianAppParams.getJSONObject("biz_params");

        /**
         * 企业id
         */
        String enterpriseId = user.getModuleId();
        /**
         * 用户id
         */
        String userId = user.getUserId();
        String instanceId = UUIDUtils.get32UUID();


        /**
         * 场景id
         */
        Long sceneId = bizParams.getLongValue("sceneId",1L);

        voiceChat.put("WakeUpQuery","开始");
        bizParams.put("coolInstanceId",instanceId);
        bailianAppParams.put("biz_params",bizParams);
        voiceChat.put("BailianAppParams",bailianAppParams.toString());
        dto.setTemplateConfig(jsonObject.toString());
        Map<String,Object> userDataMap = Maps.newHashMap();
        userDataMap.put("coolInstanceId",instanceId);
        dto.setUserData(JSON.toJSONString(userDataMap));
        SparringContext sparringContext = new SparringContext();
        sparringContext.setPoint(0);
        sparringContext.setInstanceId(instanceId);
        sparringContext.setSceneId(sceneId);
        sparringContext.setUserId(userId);
        sparringContext.setModuleId(enterpriseId);
        sparringContext.setStatus(SparringStatusEnum.ON_INIT);
        return sparringContext;
    }

}
